package getOscillogram;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: code-snippet
 * @className: BlinkWaveform
 * @description: 输入价签ID，输出LabelID闪灯波形，默认输出文件是Waveform.txt
 * @author: JLONG
 * @date: 2024/9/4 17:00
 * @version: 1.0
 */
public class BlinkWaveform {

    Map<String, String[]> labelID = new HashMap<>();

    public BlinkWaveform(String... labelIDs) {
        for (String labelID : labelIDs) {
            this.labelID.put(labelID, this.getWaveData(labelID));
        }
    }

    public static void main(String[] args) {
        BlinkWaveform waveform = new BlinkWaveform(
                "B4000025",
                "C3FF0010",
                "D0188D09",
                "E8E90825",
                "9CFF0030",
                "C0000610",
                "EEFF0010",
                "D01C1EB4",
                "B400001D",
                "C0004660",
                "EEFF0050",
                "9CFF0050",
                "E8E9099E",
                "C3FF0030",
                "CAFF0050");

        waveform.getWaveformData();
        waveform.printWaveform();
    }

    /*
     * @method: getWaveData
     * @description: 输入LabelID，输出价签闪灯二进制文件和波形
     * @param: [labelID]
     * @return: java.lang.String[] 包含转换后的二进制数据，波形和游标（字符串形式）
     * @author: JLONG
     * @date: 2024/9/6 16:38
     */
    private String[] getWaveData(String labelID) {
        String[] waveDate = new String[4];
        String checksum = "AA" + labelID + CRC8.getValue(labelID);
        StringBuilder binaryString = new StringBuilder();
        for (int i = 0; i < checksum.length(); i++) {
            // 每个字符转换为4位二进制
            int decimal = Integer.parseInt(String.valueOf(checksum.charAt(i)), 16);
            String binaryPart = String.format("%4s", Integer.toBinaryString(decimal)).replace(' ', '0');
            binaryString.append(binaryPart);
        }
        String binaryChecksum = binaryString.toString();
        String waveform = this.buildWaveform(binaryChecksum);
        waveDate[0] = checksum;
        waveDate[1] = binaryString.toString();
        waveDate[2] = waveform;
        waveDate[3] = new String(new char[binaryChecksum.length()]).replace('\0', '|');
        return waveDate;
    }

    /*
     * @method: getWaveformData
     * @description: 控制台输出价签闪灯二进制文件和波形
     * @param: []
     * @return: void
     * @author: JLONG
     * @date: 2024/9/6 16:40
     */
    public void getWaveformData() {
        for (String key : this.labelID.keySet()) {
            System.out.println("LabelID: " + key + "    ==>    " + this.labelID.get(key)[1] + "    ==>    " + this.labelID.get(key)[2] + "  ==>   " + this.labelID.get(key)[3]);
        }
    }

    /*
     * @method: printWaveform
     * @description: 将价签闪灯二进制文件和波形输出到文本文件
     * @param: []
     * @return: void
     * @author: JLONG
     * @date: 2024/9/6 16:40
     */
    public void printWaveform() {
        this.printWaveform("../Waveform.txt");
    }

    public void printWaveform(String filePath) {
        File waveFile = new File(filePath);
        if (!waveFile.exists()) {
            try {
                waveFile.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            PrintWriter writer = new PrintWriter(waveFile);
            for (String labelID : this.labelID.keySet()) {
                writer.println("价签ID： " + labelID);
                String[] data = this.labelID.get(labelID);
                for (String elem : data) {
                    writer.println(elem);
                }
                writer.println("=========================分割线=========================");
            }
            writer.flush();

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    /*
     * @method: buildWaveform
     * @description: 根据二进制字符串，转换为类波形图字符串，用于波形对比
     * @param: [binaryData]
     * @return: java.lang.String
     * @author: JLONG
     * @date: 2024/9/6 16:41
     */
    private String buildWaveform(String binaryData) {
        StringBuilder waveform = new StringBuilder();
        StringBuilder indexCursor = new StringBuilder();
        for (char bit : binaryData.toCharArray()) {
            if (bit == '1') {
                waveform.append("█");
            }
            if (bit == '0') {
                waveform.append("_");
            }
        }
        return waveform.toString();
    }
}
